﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.DictionaryMaintenance
{
    public partial class DictionaryMaintenance : Base.BasePage
    {
        ExtendBLL.Dictionary ED = new ExtendBLL.Dictionary();
        /// <summary>
        /// 用于测试的数据，删掉
        /// </summary>
        //public string TableName = "性别";//表名 ：性别
        //public string TableDes1 = "性别码";
        //public string TableDes2 = "性别名称";
        //public string DClass = "201";//类别名： SEX

        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("ZDWH");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                string Name = Server.HtmlDecode(Request.QueryString["Name"]);
                string Code = Request.QueryString["Code"];
                tbDClass.Value= Code;
                string[] Head = new string[2];
                Head = ED.GetHeaderByDClass(tbDClass.Value);
                tbTableName.Value = ED.GetNameFromDictionaryClass(tbDClass.Value);
                tbTableDes1.Value = Head[0].ToString();
                tbTableDes2.Value = Head[1].ToString();
                InitBind(tbDClass.Value);
                btnDel.Attributes.Add("onclick", "return confirm('你确定要删除?');");
            }
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="DClass"></param>
        protected void InitBind(string DClass)
        {
            string str = "DClass='" + DClass + "'";
            gvDictionary.DataSource = ED.GetModelList(str);
            gvDictionary.DataBind();
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //传递过去的值应该有：表名TableName
            //二个Lable的名字:TableDes1 TableDes2
            //分类：DClass
            string s = "" + Server.UrlEncode(tbTableName.Value) + "&TableDes1=" + Server.UrlEncode(tbTableDes1.Value) + "&TableDes2=" + Server.UrlEncode(tbTableDes2.Value) + "&DClass=" + Server.UrlEncode(tbDClass.Value) + "";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openwin", "<script type='text/javascript'>window.open('AddDictionary.aspx?TableName=" + s + "','','top=200,left=200,width=500,height=250')</script>");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDel_Click(object sender, EventArgs e)
        {
            bool success = false;
            CheckBox chk;
            for (int i = 0; i < gvDictionary.Rows.Count; i++)
            {
                chk = (CheckBox)gvDictionary.Rows[i].FindControl("chk");
                if (chk.Checked)
                {
                    int ID = int.Parse(gvDictionary.DataKeys[i].Value.ToString());
                    //执行删除
                    try
                    {
                        ED.Delete(ID);
                        success = true;
                    }
                    catch
                    {
                    }
                }
            }
            if (success == true)
            {
                Common.JShelper.JSAlert(this.Page, "", "删除成功！");
                InitBind(tbDClass.Value);
            }
        }


        protected void gvDictionary_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                string[] headers = new string[2];
                headers = ED.GetHeaderByDClass(tbDClass.Value);//根据DClass找出对应的表头显示
                e.Row.Cells[3].Text = headers[0].ToString();//更换表头HeadText 3 4
                e.Row.Cells[4].Text = headers[1].ToString();
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");

                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)//第一列的checkbox双击不会弹出，所以用循环
                {
                    e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='EditDictionary.aspx?ID=" + gvDictionary.DataKeys[gvDictionary.DataKeys.Count - 1].Value.ToString() + "'");
                }
                //e.Row.Attributes["style"] = "Cursor:hand";
                e.Row.Attributes["style"] = "Cursor:pointer";//firefox下面要用这个
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvDictionary_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDictionary.PageIndex = e.NewPageIndex;
            InitBind(tbDClass.Value);
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string str = "";
            if (tbSearch.Text.Trim() == null)
            {
                str += "1=1";
            }
            else
            {
                str += "DClass='" + tbDClass.Value + "' and Code like '%" + tbSearch.Text.Trim() + "%'";
            }
            gvDictionary.DataSource = ED.GetModelList(str);
            gvDictionary.DataBind();
        }
    }
}
